import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.DateUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hjc-YYDS
 * @date 2024/3/5 11:34
 */
@Slf4j
public class MyTest02 {

    //List<List<Object>>方式导出
    @Test
    public void testFileWrite01() {
        List<Object> list1 = Arrays.asList(1, 2, 3);
        List<Object> list2 = Arrays.asList("张飞", "关羽", "刘备");
        List<List<Object>> list = Arrays.asList(list1, list2);

        EasyExcel.write("D:\\D00_recently\\test.xlsx").
                sheet("测试").doWrite(list);
    }

    //List<List<Object>>方式导出
    @Test
    @SneakyThrows
    public void testFileWrite011() {
        List<Object> list1 = Arrays.asList("刘邦", "男", "沛县", DateUtils.parseDate("1999-01-01"));
        List<Object> list2 = Arrays.asList("刘盈", "男", "沛县", DateUtils.parseDate("2000-01-01"));
        List<Object> list3 = Arrays.asList("萧何", "男", "沛县", DateUtils.parseDate("2001-01-01"));
        List<Object> list4 = Arrays.asList("曹参", "男", "沛县", DateUtils.parseDate("2002-01-01"));
        List<Object> list5 = Arrays.asList("吕后", "女", "沛县", DateUtils.parseDate("2003-01-01"));

        List<List<Object>> list = Arrays.asList(list1, list2, list3, list4, list5);

        EasyExcel.write("D:\\D00_recently\\test2.xlsx").
                sheet("测试").doWrite(list);
    }

    //List<Map<Integer,Object>>方式导出
    @SneakyThrows
    @Test
    public void testFileWrite02() {
        Map<Integer, Object> map1 = new HashMap<>();
        map1.put(0, "1");
        map1.put(1, "刘邦");
        map1.put(2, "男");
        map1.put(3, "沛县");
        map1.put(4, DateUtils.parseDate("1999-01-01"));

        Map<Integer, Object> map2 = new HashMap<>();
        map2.put(0, "2");
        map2.put(1, "刘盈");
        map2.put(2, "男");
        map2.put(3, "沛县");
        map2.put(4, DateUtils.parseDate("2000-01-01"));

        Map<Integer, Object> map3 = new HashMap<>();
        map3.put(0, "3");
        map3.put(1, "萧何");
        map3.put(2, "男");
        map3.put(3, "沛县");
        map3.put(4, DateUtils.parseDate("2001-01-01"));

        List<Map<Integer, Object>> list = Arrays.asList(map1, map2, map3);

        EasyExcel.write("D:\\D00_recently\\test3.xlsx").
                sheet("测试").doWrite(list);

    }

    //List<List<Object>>方式导出
    @SneakyThrows
    @Test
    public void testFileWrite03() {
        User user1 = new User("刘彻", "男", "1999-01-01", "154jgfe@gmail", 99);
        User user2 = new User("主父偃", "男", "2000-01-01", "154jgfe@gmail", 99);
        User user3 = new User("司马迁", "男", "2001-01-01", "154jgfe@gmail", 99);
        User user4 = new User("董仲舒", "男", "2002-01-01", "154jgfe@gmail", 99);

        List<User> userList = Arrays.asList(user1, user2, user3, user4);

        EasyExcel.write("D:\\D00_recently\\test4.xlsx").
                sheet("测试").head(User.class).doWrite(userList);
    }
}
